﻿using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using AMS.Inventory.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using VitCode.Web.UI.WebControls;

namespace AMSInventoryWeb.BaseData
{
    public partial class ObjEdit : Page
    {
        private readonly PermissionManager psManager = new PermissionManager();
        private BaseDataManager m_Help = new BaseDataManager();
        private PageBuilder m_PageBuilder;

        public static string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\BaseDataSet.xml"; }
        }

        public string PlanState
        {
            get
            {
                if (Session["PlanState"] == null)
                    return "";
                return Session["PlanState"].ToString();
            }
            set { Session["PlanState"] = value; }
        }

        private string IsNav
        {
            get { return ReturnQueryStringValue("IsNav"); }
        }

        private string ClassName
        {
            get { return ReturnQueryStringValue("ClassName"); }
        }

        private string TypeName
        {
            get { return ReturnQueryStringValue("TypeName"); }
        }

        private string MenuId
        {
            get { return ReturnQueryStringValue("MenuId"); }
        }

        private string Operate
        {
            get { return ReturnQueryStringValue("Operate"); }
        }

        private string KeyFieldValue
        {
            get
            {
                if (ViewState["KeyFieldValue"] == null)
                    return "";
                return ViewState["KeyFieldValue"].ToString();
            }
            set
            {
                lblPageState.Text = value.Length == 0 ? "新增" : "修改";
                ViewState["KeyFieldValue"] = value;
                ctlSelected.PlanKeyVal = value;
                ctlSelect.PlanKeyVal = value;
                ctlImportor.PlanId = value;
                ucPlanInfo.PlanId = value;
            }
        }

        private void SetData()
        {
            if (KeyFieldValue.Length > 0)
            {
                object obj=m_PageBuilder.SetPageData(ClassName, TypeName, KeyFieldValue);
                if(obj!=null && obj is PlanInfo)
                {
                    PlanState = ((PlanInfo) obj).State;
                    if(PlanState != PlanInfo.STATE_NOT_START)
                        new CreateControlHelper().SetPageControlsDisabled(this);
                }
            }
            
        }

        private void SetTabCtlItem()
        {
            pgMenu.Items.Clear();

            if (ClassName == typeof (PlanInfo).Name)
            {
                pgMenu.Items.Add(new MenuItem("基本信息", "0") {Selected = true});
                pgMenu.Items.Add(new MenuItem("选择资产", "1"));
                pgMenu.Items.Add(new MenuItem("已选资产", "2"));
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(this, SettingXmlPath);
            NavigateCtl1.SettingXmlPath = SettingXmlPath;
            if (!string.IsNullOrEmpty(TypeName))
                m_PageBuilder.SetPageInfoContext(table1, ClassName, TypeName);
            else
                m_PageBuilder.SetPageInfoContext(table1, ClassName);
            if (ClassName == typeof (PlanInfo).Name)
            {
                ctlSelect.ClassName = ClassName;
                ctlSelected.ClassName = ClassName;
                ctlSelect.TypeName = TypeName;
                ctlSelected.TypeName = TypeName;
            }
            base.OnInit(e);
        }

        private string ReturnQueryStringValue(string keyString)
        {
            string strValue = Request.QueryString[keyString];
            if (strValue == null)
                strValue = "";
            return Convert.ToString(strValue);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (IsNav.Length > 0 && IsNav == "false")
                {
                    NavigateCtl1.Visible = false;
                    btnCancel.Visible = false;
                    btnOperater.Visible = false;
                }
                string strKeyField = m_PageBuilder.GetPageKeyField(ClassName);
                KeyFieldValue = ReturnQueryStringValue(strKeyField);
                if (KeyFieldValue.Length > 0)
                    SetTabCtlItem();
                SetPageTab("0");
                SetData();
                if (!psManager.CheckPageIsCanEdit(this,true))
                {
                    btnAdd.Enabled = false;
                    btnEnter.Enabled = false;
                    if(ClassName != typeof(PlanInfo).Name)
                        new CreateControlHelper().SetPageControlsDisabled(this);
                }
                if (KeyFieldValue.Length>0)
                {
                    ucPlanInfo.Visible = ClassName == typeof(PlanInfo).Name;
                    if (ucPlanInfo.Visible)
                        ucPlanInfo.SetData();
                }
                if (ClassName == typeof(InventoryInfo).Name || ClassName == typeof(LedgerInfo).Name)
                {
                    btnAdd.Enabled = false;
                    btnEnter.Enabled = false;
                    new CreateControlHelper().SetPageControlsDisabled(this);
                }
                if(PlanState == "Finish" || PlanState == "Doing")
                {
                    ctlImportor.Visible = false;
                }
                if (Operate == "Add")
                    btnAdd.Visible = false;
            }
        }

        protected void btnEnter_Click(object sender, EventArgs e)
        {
            var saveHelper = new PageSaveHelper(this, SettingXmlPath);
            object obj;
            StringCollection scMsg;
            obj = saveHelper.SavePageObject(ClassName, TypeName, KeyFieldValue, out scMsg);

            if (scMsg.Count > 0)
            {
                var sb = new StringBuilder();
                for (int i = 0; i < scMsg.Count; i++)
                    sb.Append(scMsg[i] + "\r\n");
                Alert.MessageBox(sb.ToString());
            }
            else
            {
                string strKeyField = m_PageBuilder.GetPageKeyField(ClassName);
                PropertyInfo pi = obj.GetType().GetProperty(strKeyField);
                KeyFieldValue = pi.GetValue(obj, null).ToString();
                Alert.MessageBox("保存成功!");
                if (pgMenu.Items.Count == 0)
                {
                    SetTabCtlItem();
                    SetPageTab("0");
                }
                SetData();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Window.Close();
           // Response.Redirect("ObjList.aspx?MenuId=" + MenuId + "&ClassName=" + ClassName + "&TypeName=" + TypeName);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            KeyFieldValue = "";
            SetData();
        }

        private void SetPageTab(string itemVal)
        {
            divBaseInfo.Visible = false;
            divSelectInfo.Visible = false;
            divSelectedInfo.Visible = false;

            if (itemVal == "0")
            {
                divBaseInfo.Visible = true;
            }
            else if (itemVal == "1")
            {
                divSelectInfo.Visible = true;
                if (!string.IsNullOrEmpty(PlanState) && PlanState != PlanInfo.STATE_NOT_START)
                    ctlSelect.SetCtlDisabled();
            }
            else if (itemVal == "2")
            {
                divSelectedInfo.Visible = true;
                if (!string.IsNullOrEmpty(PlanState) && PlanState != PlanInfo.STATE_NOT_START)
                {
                    ctlSelected.SetCtlDisabled();
                    ctlImportor.SetCtlDisabled();
                }
            }
            
        }

        protected void pgMenu_MenuItemClick(object sender, MenuEventArgs e)
        {
            SetPageTab(e.Item.Value);
        }
    }
}